<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
	use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

	protected $per_page;
	protected $user;
	protected $request;
	protected $word;

	public function __construct(Request $request)
	{
		$this->request  = $request;
		$this->user     = $request->user();
		$this->per_page = $request->get('per_page', 15);
		$this->word     = $request->get('word');
	}

	protected function items($paginator, $other = [])
	{
		$data = $paginator->items();
		$meta = [
			'pagination' => [
				'current_page' => (int)$paginator->currentPage(),
				'per_page'     => (int)$paginator->perPage(),
				'total'        => (int)$paginator->total()
			]
		];
		if (!empty($other)) {
			$meta = array_merge($meta, $other);
		}
		return response()->json([
			'code' => 0,
			'data' => $data,
			'meta' => $meta,
			'msg'  => 'Success'
		]);
	}

	protected function success($data = null, $flag = 0, $msg = 'Success')
	{
		return response()->json([
			'flag' => $flag,
			'data' => $data,
			'msg'  => $msg,
		]);
	}

	protected function failed($msg = 'Error', $flag = 10001, $data = null)
	{
		return response()->json([
			'flag' => $flag,
			'data' => $data,
			'msg'  => $msg,
		]);
	}
}
